package com.tangsm.spring.boot.api.controller;

import com.tangsm.spring.boot.api.base.Result;
import com.tangsm.spring.boot.api.enums.BusinessResultCode;
import com.tangsm.spring.boot.api.enums.ResultCode;
import com.tangsm.spring.boot.api.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 自定义业务异常测试
 */
@Api(tags = "自定义业务异常测试")
@RestController
@RequestMapping("/except")
public class ApiExceptionController {
    private static final Logger log = LoggerFactory.getLogger(ApiExceptionController.class);

    @Value("${api.test-param}")
    private String testParam;

    /**
     * 无参业务异常测试
     *
     * @param type 请求类型，0-异常响应，1-正常响应
     * @return 响应结果
     */
    @ApiOperation("无参业务异常测试")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "请求类型", dataType = "Long", dataTypeClass = Long.class, value = "请求类型[0-异常响应,1-正常响应]")
    )
    @ApiResponses(
            value = {
                    @ApiResponse(code = 200, message = "操作成功"),
                    @ApiResponse(code = 500, message = "服务异常")
            }
    )
    @GetMapping("/business")
    public Result<?> businessException(Long type) {
        log.info("有参业务异常测试，请求参数：{}", type);
        if (type == null || type == 0) {
            throw new BusinessException();
        }
        return Result.success();
    }

    /**
     * 有参业务异常测试
     *
     * @param type 请求类型，0-异常响应，1-正常响应
     * @return 响应结果
     */
    @ApiOperation("有参业务异常测试")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "请求类型", dataType = "Long", dataTypeClass = Long.class, value = "请求类型[0-异常响应,1-正常响应]")
    )
    @ApiResponses(
            value = {
                    @ApiResponse(code = 200, message = "操作成功"),
                    @ApiResponse(code = 4000, message = "参数错误")
            }
    )
    @GetMapping("/paramBusiness")
    public Result<?> paramBusinessException(Long type) {
        log.info("有参业务异常测试，请求参数：{}", type);
        if (type == null || type == 0) {
            throw new BusinessException(ResultCode.ERROR);
        }
        return Result.success();
    }

    /**
     * 可变长参数业务异常测试
     *
     * @param type 请求类型，0-异常响应，1-正常响应
     * @return 响应结果
     */
    @ApiOperation("可变长参数业务异常测试")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "请求类型", dataType = "Long", dataTypeClass = Long.class, value = "请求类型[0-异常响应,1-正常响应]")
    )
    @ApiResponses(
            value = {
                    @ApiResponse(code = 200, message = "操作成功"),
                    @ApiResponse(code = 19002, message = "占位符测试")
            }
    )
    @GetMapping("/longParamBusiness")
    public Result<?> longParamBusinessException(Long type) {
        log.info("可变长参数业务异常测试，请求参数：{}", type);
        if (type == null || type == 0) {
            throw new BusinessException(BusinessResultCode.PLACEHOLDER_ERROR, "小美");
        }
        return Result.success();
    }

    /**
     * 全局通用异常测试
     *
     * @param type 请求类型，0-异常响应，1-正常响应
     * @return 响应结果
     */
    @ApiOperation("全局通用异常测试")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "请求类型", dataType = "Long", dataTypeClass = Long.class, value = "请求类型[0-异常响应,1-正常响应]")
    )
    @ApiResponses(
            value = {
                    @ApiResponse(code = 200, message = "操作成功"),
                    @ApiResponse(code = 500, message = "服务异常")
            }
    )
    @GetMapping("/globalException")
    public Result<?> globalException(Long type) {
        log.info("全局通用异常测试，请求参数：{}", type);
        log.info("环境参数：{}", this.testParam);
        int i = 0;
        if (type == null || type == 0) {
            i = 1 / 0;
        }
        return Result.success(i);
    }
}
